<template>
  <div class="app-container">
    <el-input v-model="filterText" placeholder="Filter keyword" style="margin-bottom:30px;"/>

    <el-tree
      ref="subject"
      :data="subjectList"
      :props="defaultProps"
      :filter-node-method="filterNode"
      class="filter-tree"
      default-expand-all
    />

  </div>
</template>

<script>
  import subject from "@/api/teach/subject";

  export default {

    data() {
      return {
        filterText: '',
        subjectList: [],//分类数据列表
        defaultProps: {
          children: 'children',
          label: 'title'
        }
      }
    },
    watch: {
      filterText(val) {
        this.$refs.subject.filter(val)
      }
    },
    created() {
      //初始化分类数据
      this.getSubjectList()
    },

    methods: {
      //获取所有分类
      getSubjectList() {
        //调用api接口方法
        subject.querySubjectList()
          .then(response => {
            if (response.success === true) {
              //将后端返回的值赋值给分类数据列表
              this.subjectList = response.data.subjectVoList
            }
          })
      },
      //搜索方法
      filterNode(value, data) {
        if (!value) return true
        return data.title.toLowerCase().indexOf(value.toLowerCase()) !== -1
      }
    }
  }
</script>

